﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinalTestMachine.View.MainUnits
{
    /// <summary>
    /// socket参数对象类
    /// </summary>
    public class Param_Socket : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string sn;
        private string id;

        public string SN
        {
            get { return sn; }
            set
            {
                sn = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SN"));
            }
        }

        public string ID
        {
            get { return id; }
            set
            {
                id = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ID"));
            }
        }
    }

    /// <summary>
    /// Socket SN 参数设置
    /// </summary>
    public  class SocketSNSetting
    {
        public static readonly SocketSNSetting Cur = new SocketSNSetting();
        string SavePath => System.IO.Path.Combine(Motion.ParamPath_Motion.SelectedDirPath, "SocketSeting.ini");

        private SocketSNSetting()
        {
            saveRead = new SaveReaddll.SaveReadHeler(() => SavePath, () => this);
            if (System.IO.File.Exists(SavePath))
            {
                Read();
            }
            else
            {
                using (System.IO.File.Create(SavePath)) { }
            }

            Connect.ConnectFactory.TCP_ScanSocket.RecevieMsgActionEventHander += SerialPort_ScanSocket_RecevieMsgActionEventHander;
        }

        private void SerialPort_ScanSocket_RecevieMsgActionEventHander(string obj)
        {
            SN = obj;
        }

        /// <summary>
        ///扫描治具的SN
        /// </summary>
        public string SN { get; set; }

        SaveReaddll.SaveReadHeler saveRead;

        [ISaveReaddll.SaveRemark(ISaveReaddll.ReadWriteWay.Direct)]
        public readonly ObservableCollection<Param_Socket> SocketList = new ObservableCollection<Param_Socket>();


        public void Save()
        {
            saveRead?.Save();
        }

        public void Read()
        {
            saveRead?.Read();
        }

        /// <summary>
        /// 增加一个治具对应
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="id"></param>
        public void Add(string sn,string id)
        {
            //检查是否已经保护
            foreach (var item in SocketList)
            {
                if(item.SN==sn)
                {
                    throw new Exception("不能添加重复的Socket编码");
                }
                if(item.ID==id)
                {
                    throw new Exception("不能添加重复的ID");
                }
            }
            SocketList.Add(new Param_Socket() { SN = sn, ID = id });
        }

        public void Remov(Param_Socket param_Socket)
        {
            if(SocketList.Contains(param_Socket))
            {
                SocketList.Remove(param_Socket);
            }
        }


        public string GetSocketSN(string id)
        {
            foreach (var item in SocketList)
            {

                if (item.ID == id)
                {
                    return item.SN;
                }
            }

            return "";
        }
    }
}
